The below worked fine for me:
<xp:eventHandler event="onchange" submit="false">
<xp:this.script>
<xp:executeClientScript>
<xp:this.script><![CDATA[
var input = dojo.byId('#{id:inputText1}');
if(input.value == "example")
{
alert("ERROR");
input.focus();
}
]]></xp:this.script>
</xp:executeClientScript>
</xp:this.script>
</xp:eventHandler>
however if this is for a form entry field and you have access to the ExtLib that comes from openNTF.org or passport advantage, I would suggest using the formTable control and a validator. Validators are already inbuilt, you can apply them to a form element and it won't allow form submission until the issue is resolved. The reason the form table is good is because it will display a yellow box on top of the control with a warning message and a list of all the validation fails and place a red X on top of each field that has failed. It adds a lot of functionality.
e.g.
<xe:formRow labelPosition="left" label="test">
<xp:inputText id="inputText1">
<xp:this.validators>
<xp:validateExpression message="Failed">
<xp:this.expression><![CDATA[#{javascript:
if(getComponent('inputText1').getValue() == "example")
{
return true;
}
else
{
return false;
}
}]]></xp:this.expression>
</xp:validateExpression>
</xp:this.validators>
</xp:inputText>
</xe:formRow>